﻿//using System;
//using System.Collections.Generic;
//using System.Linq;
//using System.Text;
//using System.IO;
//using MusicBrowser.Providers;

//namespace MusicBrowser.Metadata.LastFM
//{
//    class ScrobbleService
//    {
//        public enum ScrobbleTarget
//        {
//            Web,
//            File
//        }

//        public struct ScrobbleDTO
//        {
//            public string track;
//            public string artist;
//            public string album;
//            public string mbid;
//            public Int32 duration;
//            public string sessionKey;

//            public ScrobbleTarget target;
//        }

//        private const string LOGFILE = "C:\\.scrobbler.log";

//        public static void DoService(ScrobbleDTO DTO)
//        {
//            if (String.IsNullOrEmpty(DTO.track) || String.IsNullOrEmpty(DTO.artist))
//            {
//                Logging.LoggerFactory.SelectedLogger.LogInfo("Track is missing info required to update Last.FM");
//                return;
//            }

//            if (DTO.target == ScrobbleTarget.File) { ScrobbleToFile(DTO); }
//            if (DTO.target == ScrobbleTarget.Web) { ScrobbleToWeb(ref DTO); }
//        }

//        private static void ScrobbleToWeb(ref ScrobbleDTO DTO)
//        {
//            //Scrobble track.scrobble
//            //api_sig (Required) : A Last.fm method signature. See authentication for more information.
//            //api_key (Required) : A Last.fm API key.
//            //track[i] (Required) : The track name.
//            //timestamp[i] (Required) : The time the track started playing, in UNIX timestamp format (integer number of seconds since 00:00:00, January 1st 1970 UTC). This must be in the UTC time zone.
//            //artist[i] (Required) : The artist name.
//            //album[i] (Optional) : The album name.
//            //albumArtist[i] (Optional) : The album artist - if this differs from the track artist.
//            //trackNumber[i] (Optional) : The track number of the track on the album.
//            //mbid[i] (Optional) : The MusicBrainz Track ID.
//            //duration[i] (Optional) : The length of the track in seconds.
//            //sk (Required) : A session key generated by authenticating a user via the authentication protocol.

//            TimeSpan ts = (DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0));
//            Int32 unixTime = (Int32)ts.TotalSeconds - (DTO.duration / 2);

//            LastFMProvider.LFMRequest request = new LastFMProvider.LFMRequest();
//            request.RequiresSignature = true;
//            request.Params = new SortedDictionary<string, string>();
//            request.Params.Add("method", "track.scrobble");
//            request.Params.Add("track", DTO.track);
//            request.Params.Add("timestamp", unixTime.ToString());
//            request.Params.Add("artist", DTO.artist);
//            request.Params.Add("album", DTO.album);
//            request.Params.Add("mbid", DTO.mbid);
//            request.Params.Add("duration", DTO.duration.ToString());
//            request.Params.Add("sk", DTO.sessionKey);

//            LastFMProvider.AccessLastFM(request);
//        }

//        private static void ScrobbleToFile(ScrobbleDTO DTO)
//        {
//            if (Util.Config.Instance.getBooleanSetting("ScrobbleToFile"))
//            {
//                try
//                {
//                    if (!File.Exists(LOGFILE))
//                    {
//                        WriteToFile(LOGFILE, "#AUDIOSCROBBLER/1.1");
//                        WriteToFile(LOGFILE, "#TZ/UTC");
//                        WriteToFile(LOGFILE, "#CLIENT/MusicBrowser 2");
//                    }

//                    TimeSpan ts = (DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0));
//                    Int32 unixTime = (Int32)ts.TotalSeconds - (DTO.duration / 2);

//                    WriteToFile(
//                        LOGFILE,
//                        String.Format("{0}\t{1}\t{2}\t{3}\t{4}\t{5}\t{6}\t{7}",
//                        DTO.artist,
//                        DTO.album,
//                        DTO.track,
//                        string.Empty,
//                        DTO.duration.ToString(),
//                        'L',
//                        unixTime.ToString(),
//                        DTO.mbid
//                    ));

//                }
//                catch (Exception Ex)
//                {
//                    Logging.LoggerFactory.SelectedLogger.LogError(new Exception("failed to write to scrobble log", Ex));
//                }
//            }
//        }

//        private static void WriteToFile(string filename, string content)
//        {
//            try
//            {
//                StreamWriter fs = File.AppendText(filename);
//                fs.WriteLine(content);
//                fs.Flush();
//                fs.Close();
//            }
//            catch { }
//        }
//    }
//}